<template>
  <div class="app-container">
    <el-form ref="form" :model="form" label-width="80px" size="small">
      <el-form-item label="角色名称">
        <el-input
          v-model="form.roleName"
          placeholder="请输入角色名称"
          size="small"
          style="width: 200px"
        ></el-input>
        <el-button type="primary" @click="handleSearch">查询</el-button>
      </el-form-item>
    </el-form>

    <el-row style="margin-bottom: 10px">
      <el-button type="primary" size="mini" @click="handleAddRole"
        >新增</el-button
      >
      <el-button type="danger" size="mini" @click="handleDeleteRole"
        >删除</el-button
      >
    </el-row>

    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column label="角色名称">
        <template slot-scope="scope">{{ scope.row.roleName }}</template>
      </el-table-column>
      <el-table-column label="角色编码">
        <template slot-scope="scope">{{ scope.row.roleCode }}</template>
      </el-table-column>
      <el-table-column label="备注">
        <template slot-scope="scope">{{ scope.row.descr }}</template>
      </el-table-column>
      <el-table-column label="创建人">
        <template slot-scope="scope">{{ scope.row.createUser }}</template>
      </el-table-column>
      <el-table-column label="创建时间">
        <template slot-scope="scope">{{ scope.row.createTime }}</template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <!-- <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">查看</el-button> -->
          <el-button size="mini" @click="handleEditRole(scope.row)"
            >编辑</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      layout="total, sizes, prev, pager, next, jumper"
      :page-sizes="pageSizes"
      :current-page="currentPage"
      :page-size="pageSize"
      :total="total"
    ></el-pagination>

    <!-- 角色新增 -->
    <RoleAdd
      @success="onSuccess"
      @failed="onFailed($event)"
      @cancel="onCancel"
      @close="onClose"
      :roleAddDialogVisible="roleAdd.visible"
    ></RoleAdd>

    <!-- 角色编辑 -->
    <RoleEdit
      ref="roleEdit"
      @success="onEditRoleSuccess"
      @failed="onEditRoleFailed($event)"
      @cancel="onEditRoleCancel"
      @close="onEditRoleClose"
      :roleEditDialogVisible="roleEdit.visible"
    ></RoleEdit>
  </div>
</template>

<script>
import { getList } from "@/api/role";
import RoleAdd from "@/views/system/role/RoleAdd";
import RoleEdit from "@/views/system/role/RoleEdit";

export default {
  name: "Role",
  components: { RoleAdd, RoleEdit },
  data() {
    return {
      list: null,
      listLoading: true,
      form: {
        roleName: "",
      },
      pageSizes: [10, 20, 30, 40],
      currentPage: 1,
      pageSize: 10,
      total: 0,
      roleAdd: {
        visible: false,
      },
      roleEdit: {
        visible: false,
      },
    };
  },
  created() {
    this.fetchData();
  },
  methods: {
    fetchData() {
      this.listLoading = true;
      var params = {
        roleName: this.form.roleName,
        pageNum: this.currentPage,
        pageSize: this.pageSize,
      };

      getList(params).then((response) => {
        if (response.data) {
          this.list = response.data.roles;
          this.total = response.data.total;
        } else {
          this.list = [];
          this.total = 0;
        }
        this.listLoading = false;
      });
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.fetchData();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentPage = val;
      this.fetchData();
    },
    handleSearch() {
      this.fetchData();
    },
    handleAddRole() {
      // this.visible2 = false;
      this.roleAdd.visible = true;
    },
    handleDeleteRole() {},
    onSuccess() {
      this.roleAdd.visible = false;
      // 添加成功
      this.$message({
        message: "新增角色成功！",
        type: "success",
      });
      this.fetchData();
    },
    onFailed(msg) {
      this.roleAdd.visible = false;
      this.$message.error("新增角色失败！" + msg);
    },
    onCancel() {
      this.roleAdd.visible = false;
    },
    onClose() {
      this.roleAdd.visible = false;
    },
    handleEditRole(record) {
      console.log("record="+JSON.stringify(record))
      // this.visible2 = false;
      this.roleEdit.visible = true;
      this.$refs.roleEdit.setFormValues(record);
    },
    onEditRoleSuccess() {
      this.roleEdit.visible = false;
      // 添加成功
      this.$message({
        message: "编辑角色成功！",
        type: "success",
      });
      this.fetchData();
    },
    onEditRoleFailed(msg) {
      this.roleEdit.visible = false;
      this.$message.error("编辑角色失败！" + msg);
    },
    onEditRoleCancel() {
      this.roleEdit.visible = false;
    },
    onEditRoleClose() {
      this.roleEdit.visible = false;
    },
  },
};
</script>

